/**
 * 
 */
package org.sword.wechat4j.token;

import org.sword.wechat4j.common.WechatConfig;
import org.sword.wechat4j.token.server.AccessTokenServer;
import org.sword.wechat4j.token.server.JsApiTicketServer;
import org.sword.wechat4j.token.server.TokenServer;

/**
 * AccessToken代理 所有获取accessToken的地方都通过此代理获得 获得方法String token =
 * AccessTokenProxy.token()
 * 
 * @author ChengNing
 * @date 2015年1月9日
 */
public class TokenProxy {

	protected final WechatConfig wechatConfig;

	private TokenServer accessTokenServer;

	private JsApiTicketServer ticketServer;

	public TokenProxy(WechatConfig wechatConfig) {
		super();
		this.wechatConfig = wechatConfig;
	}

	public WechatConfig getWechatConfig() {
		return wechatConfig;
	}

	/**
	 * 通过代理得到accessToken的串
	 */
	public String accessToken() {
		if (this.accessTokenServer == null) {
			this.accessTokenServer = new AccessTokenServer(wechatConfig);
		}
		return accessTokenServer.token();
	}

	/**
	 * 通过代理得到jsapi_ticket
	 */
	public String jsApiTicket() {
		if (this.accessTokenServer == null) {
			this.accessTokenServer = new AccessTokenServer(wechatConfig);
		}
		if (this.ticketServer == null) {
			this.ticketServer = new JsApiTicketServer(accessTokenServer.token(), wechatConfig);
		}
		return ticketServer.ticket();
	}

}
